package controllers

import (
	"bee-fisco-api/helloworld"
	"fmt"
	"github.com/FISCO-BCOS/go-sdk/client"
	"github.com/astaxie/beego"
	"github.com/ethereum/go-ethereum/common"
	"log"
)

// HelloController operations for Hello
type HelloController struct {
	beego.Controller
	Cli *client.Client
}

// URLMapping ...
func (c *HelloController) URLMapping() {
	fmt.Println("begin URLMapping")
	c.Mapping("Post", c.Post)
	c.Mapping("Get", c.Get)
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
	fmt.Println("end URLMapping")
}

// Post ...
// @Title Create
// @Description create Hello
// @Param	body		body 	models.Hello	true		"body for Hello content"
// @Success 201 {object} models.Hello
// @Failure 403 body is empty
// @router / [post]
func (c *HelloController) Post() {
	c.Data["json"] = map[string]string{"hello": "world"}
	c.ServeJSON()
}

func (c *HelloController) Get() {

	//c.Ctx.WriteString("hello world")
	//获取合约部署地址（从哪里获取？）
	contractAddress := common.HexToAddress("0xe5708ed2108bb3d4dc738f18dca72bbae10dcf4d") // 0x481D3A1dcD72cD618Ea768b3FbF69D78B46995b0

	//获取合约实例对象
	instance, err := helloworld.NewHelloWorld(contractAddress, c.Cli)
	if err != nil {
		log.Fatal(err)
	}

	//获取合约会话对象
	helloworldSession := &helloworld.HelloWorldSession{Contract: instance, CallOpts: *c.Cli.GetCallOpts(), TransactOpts: *c.Cli.GetTransactOpts()}

	//调用合约方法
	value, err := helloworldSession.Get()
	if err != nil {
		log.Fatal(err)
	}

	c.Ctx.WriteString(value)
}

// GetOne ...
// @Title GetOne
// @Description get Hello by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Hello
// @Failure 403 :id is empty
// @router /:id [get]
func (c *HelloController) GetOne() {

}

// GetAll ...
// @Title GetAll
// @Description get Hello
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 {object} models.Hello
// @Failure 403
// @router / [get]
func (c *HelloController) GetAll() {
	//c.Data["json"] = map[string]string{"hello": "world"}
	//c.ServeJSON()
	c.Ctx.WriteString("hello world")
}

// Put ...
// @Title Put
// @Description update the Hello
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.Hello	true		"body for Hello content"
// @Success 200 {object} models.Hello
// @Failure 403 :id is not int
// @router /:id [put]
func (c *HelloController) Put() {

}

// Delete ...
// @Title Delete
// @Description delete the Hello
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *HelloController) Delete() {

}
